package com.xea.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import com.octo.captcha.service.CaptchaServiceException;
import com.xea.model.UserInfo;
import com.xea.service.ServiceFactory;
import com.xea.service.UserInfoService;
import com.xea.util.CaptchaServiceSingleton;
import com.xea.util.MD5Parse;
import com.xea.util.StringUtil;

public class UserInfoAction extends DispatcherServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = -9202567560257936161L;

	
	/**
	 * 用户登录
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	public void login(HttpServletRequest request,HttpServletResponse response) throws IOException{
		String username = request.getParameter("username");
		String temp_password = request.getParameter("password");
		MD5Parse mp = new MD5Parse();
		String password = mp.getMD5ofStr(temp_password);
		UserInfoService userInfoDao = ServiceFactory.getUserInfoService();
		Boolean isResponseCorrect = Boolean.FALSE;
		String captchaId = request.getSession().getId();
		String vcode = request.getParameter("vcode");
		response.setCharacterEncoding("utf-8");
		JSONObject json=new JSONObject();
		try {
			isResponseCorrect = CaptchaServiceSingleton.getInstance().validateResponseForID(captchaId, vcode);
			if(!isResponseCorrect){
				PrintWriter out = response.getWriter();
				json.put("msg", "验证码有误,请重新登录！");
				out.print(json.toString());
				out.close();
				return;
			}
		} catch (CaptchaServiceException e) {
			PrintWriter out = response.getWriter();
			json.put("msg", "验证码有误,请重新登录！");
			out.print(json.toString());
			out.close();
			return;
		}
		UserInfo user = userInfoDao.find(username, password);
		if(user == null){
			PrintWriter out = response.getWriter();
			json.put("msg", "用户名或密码错误,请重新登录！");
			out.print(json.toString());
			out.close();
			return;
		}
		request.getSession().setAttribute("user", user);
	}
	
	/**
	 * 用户注销
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	public void logout(HttpServletRequest request,HttpServletResponse response) throws IOException{
		request.getSession().setAttribute("user",null);
		request.getSession().removeAttribute("user");
		request.getSession().invalidate();
		response.sendRedirect(request.getContextPath()+ "/login.jsp");
			
	}
	
	/**
	 * 查询所有用户
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException 
	 */
	public void search(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException{
		UserInfoService userInfoDao = ServiceFactory.getUserInfoService();
		String username=request.getParameter("username");
		if(StringUtil.isNullOrEmpty(username))
			username="";
		String pageNo=request.getParameter("pageNo");
		if(StringUtil.isNullOrEmpty(pageNo))
			pageNo="";
		request.setAttribute("pagedata", userInfoDao.search(username, pageNo));
		response.setCharacterEncoding("utf-8");
		RequestDispatcher dispatcher=request.getRequestDispatcher("inner/admin/userinfo/list.jsp");
		dispatcher.forward(request, response);
	}
	
	/**
	 * 新增用户
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	public void add(HttpServletRequest request,HttpServletResponse response) throws IOException{
		UserInfoService userInfoDao = ServiceFactory.getUserInfoService();
		String name=request.getParameter("name");
		String username=request.getParameter("username");
		String type=request.getParameter("type");
		String tel=request.getParameter("tel");
		String temp_password=request.getParameter("password");
		MD5Parse mp = new MD5Parse();
		String password=mp.getMD5ofStr(temp_password);
		String remark=request.getParameter("remark");
		response.setCharacterEncoding("utf-8");
		JSONObject json=new JSONObject();
		if(userInfoDao.find(username)!=null){
			PrintWriter out = response.getWriter();
			json.put("msg", "该用户名存在,请重新输入!");
			out.print(json.toString());
			out.close();
			return;
		}
		UserInfo userinfo=new UserInfo(name, username, password, tel,Integer.valueOf(type), remark);
		userInfoDao.add(userinfo);
	}
	
	/**
	 * 删除用户
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	public void del(HttpServletRequest request,HttpServletResponse response) throws IOException{
		UserInfoService userInfoDao = ServiceFactory.getUserInfoService();
		String id=request.getParameter("id");
		userInfoDao.del(id);
	}
	
	/**
	 * 查看用户详情
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException 
	 */
	public void detail(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException{
		UserInfoService userInfoDao = ServiceFactory.getUserInfoService();
		String id=request.getParameter("id");
		request.setAttribute("userinfo", userInfoDao.findById(Integer.valueOf(id)));
		response.setCharacterEncoding("utf-8");
		RequestDispatcher dispatcher=request.getRequestDispatcher("inner/admin/userinfo/detail.jsp");
		dispatcher.forward(request, response);
	}
	
	/**
	 * 查询用户详情
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException 
	 */
	public void findById(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException{
		UserInfoService userInfoDao = ServiceFactory.getUserInfoService();
		String id=request.getParameter("id");
		request.setAttribute("userinfo", userInfoDao.findById(Integer.valueOf(id)));
		response.setCharacterEncoding("utf-8");
		RequestDispatcher dispatcher=request.getRequestDispatcher(request.getParameter("_url"));
		dispatcher.forward(request, response);
	}
	
	/**
	 * 修改用户
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	public void update(HttpServletRequest request,HttpServletResponse response) throws IOException{
		UserInfoService userInfoDao = ServiceFactory.getUserInfoService();
		String name=request.getParameter("name");
		String username=request.getParameter("username");
		String type=request.getParameter("type");
		String tel=request.getParameter("tel");
		String id=request.getParameter("id");
		String remark=request.getParameter("remark");
		response.setCharacterEncoding("utf-8");
		JSONObject json=new JSONObject();
		UserInfo userinfo=userInfoDao.find(username,Integer.valueOf(id));
		if(userinfo!=null){
			PrintWriter out = response.getWriter();
			json.put("msg", "该用户名已存在,请重新输入!");
			out.print(json.toString());
			out.close();
			return;
		}
		UserInfo user=userInfoDao.findById(Integer.valueOf(id));
		user.setName(name);
		user.setUsername(username);
		user.setType(Integer.valueOf(type));
		user.setTel(tel);
		user.setRemark(remark);
		userInfoDao.update(user);
	}
	
	/**
	 * 修改密码
	 * @param request
	 * @param response
	 * @throws IOException 
	 */			
	public void updatepassword(HttpServletRequest request,HttpServletResponse response) throws IOException{
		UserInfoService userInfoDao = ServiceFactory.getUserInfoService();
		String temp_password = request.getParameter("password");
		String id = request.getParameter("id");
		MD5Parse mp = new MD5Parse();
		String password = mp.getMD5ofStr(temp_password);
		UserInfo user=userInfoDao.findById(Integer.valueOf(id));
		user.setPassword(password);
		userInfoDao.updatepassword(user);
	}
}	
